package com.zsc.newbuds.service.impl;

import com.zsc.newbuds.entity.Admin;
import com.zsc.newbuds.entity.Shop;
import com.zsc.newbuds.entity.User;
import com.zsc.newbuds.mapper.UserMapper;
import com.zsc.newbuds.service.UserService;
import com.zsc.newbuds.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author D
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisUtil redisUtil;
    @Override
    public User getUser(String phone) {
        return userMapper.getUser(phone);
    }


    @Override
    public void addUser(User user) {
        String key = "list:userList";
        userMapper.addUser(user);
        redisUtil.hset(key,user.getUserId().toString(),user);
    }

    @Override
    public int isExist(String phone) {
        int flag = 0;
        String key = "list:userList";
        if (redisUtil.hasKey(key)){
            Map<Object, Object> map = redisUtil.hmget(key);
            for(Map.Entry<Object,Object> u:map.entrySet()){
                User user = (User) u.getValue();
                if(user.getPhone().equals(phone)){
                    System.out.println("存在");
                    flag = 1;
                    break;
                }
            }
        }else {
            User user = userMapper.getUser(phone);
            if(user!=null){
                flag = 1;
            }
        }
        return flag;
    }

    @Override
    public void addVolunteerId(Integer userId, Integer volunteerId) {
        userMapper.addVolunteerId(userId, volunteerId);
    }

    @Override
    public void deleteVolunteer(int volunteerId) {
        Integer userId = userMapper.getUserId(volunteerId);
        System.out.println(userId);
        //删除志愿者账号
        userMapper.addVolunteerId(userId,0);
    }


    @Override
    public void updateUserPassword(Integer userId,String password){
        System.out.println("用户修改密码");
        userMapper.updateUserPassword(userId,password);
    }

    @Override
    public void updateUserInfo(Integer userId,String username,String address){
        System.out.println("更新个人信息");
        userMapper.updateUserInfo(userId,username,address);
    }

    @Override
    public void updateUserPhoneNumber(Integer userId, String phone) {
        System.out.println("修改手机号");
        userMapper.updateUserPhone(userId,phone);
    }

    @Override
    public void updateUserIntegral(Integer userId, Integer goodsId) {
        Shop shop = (Shop) redisUtil.hget("list:shopList",goodsId.toString());
        System.out.println(shop);
        User user = new User();
        String userListKey = "list:userList";
        String userKey = "user:"+userId.toString();
        if(redisUtil.hasKey(userKey)){
            System.out.println("userKey中获取");
            user = (User) redisUtil.get(userKey);
        }else if(redisUtil.hasKey(userListKey)){
            System.out.println("userListKey中获取");
            user = (User)redisUtil.hget(userListKey,userId.toString());
        }else {
            user = userMapper.getUserById(userId);
        }
        int newIntegral  = user.getIntegral()  - shop.getGoodsPrice();
        System.out.println(newIntegral);
        user.setIntegral(newIntegral);
        System.out.println(user.getIntegral()+"积分数量");
        redisUtil.set(userKey,user);
        redisUtil.hset(userListKey,userId.toString(),user);
        userMapper.updateUserIntegral(userId,newIntegral);
    }

    @Override
    public List<User> getUserList() {
        List<User> users = new ArrayList<>();
        String key = "list:userList";
        if(redisUtil.hasKey(key)){
            Map<Object, Object> map = redisUtil.hmget(key);
            for(Map.Entry<Object,Object> user:map.entrySet()){
                users.add((User)user.getValue());
            }
        }else {
            Map< String,Object> map = new HashMap<>();
            users = userMapper.getUserList();
            for (User user : users){
                map.put(user.getUserId().toString(),user);
            }
            redisUtil.hmset(key,map);
        }
        return users;
    }

    @Override
    public User getUserInfo(Integer userId) {
        User user = new User();
        String key = "list:userList";
        if(redisUtil.hasKey(key)){
            user = (User) redisUtil.hget(key,userId.toString());
        }else {
            user = userMapper.getUserById(userId);
        }

        return user;
    }

    @Override
    public void deleteUser(Integer userId) {
        String key = "user:"+userId.toString();
        String keyList = "list:userList";
        redisUtil.del(key);
        redisUtil.hdel(keyList,userId.toString());
        userMapper.deleteUser(userId);
    }
}
